package com.test;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

import java.util.Set;
import java.util.concurrent.TimeUnit;

public class musicTestFinal {
    /**
     * 遍历句柄
     * @param h
     * @return
     */
    public static String[] setArray(Set<String> h) {
        String[] b = new String[2];
        for (String a : h) {
            int i = 0;
            b[i] = a;
            i++;
        }
        return b;
    }

    /**
     * xpath点击方法
     * @param driver
     * @param a
     * @throws InterruptedException
     */
    public static void xpathClick(WebDriver driver, String a) throws InterruptedException {
        driver.findElement(By.xpath(a)).click();
        Thread.sleep(2000);
    }

    /**
     * xpath键入内容方法
     * @param driver
     * @param a
     * @param b
     * @throws InterruptedException
     */
    public static void xpathSendKeys(WebDriver driver, String a, String b) throws InterruptedException {
        driver.findElement(By.xpath(a)).sendKeys(b);
        Thread.sleep(2000);
    }

    public static void test(WebDriver driver, Actions action) throws InterruptedException {
        //在driver实例化完成后设置隐式等待，设置超时时间5s
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        //窗口最大化，防止元素隐藏
        driver.manage().window().maximize();
        //打开网易云音乐首页
        driver.get("https://music.163.com/");
        Thread.sleep(2000);
        /*
         * scrollTo滚动到指定位置;
         * scrollBy指定滚动方向和滚动量;
         */
//    	((JavascriptExecutor)driver).executeScript("window.scrollBy(0,-200);");
//    	((JavascriptExecutor)driver).executeScript("window.scrollTo(0,300);");
//    	action.sendKeys(Keys.END).perform();

        //1. 登录网易云首页，点击“登录”按钮，然后选择自己的登录方式登录。登录完成之后点击“排行榜”按钮。
        driver.findElement(By.partialLinkText("登录")).click();
        Thread.sleep(1000);
        driver.findElement(By.partialLinkText("选择其他登录模式")).click();
        Thread.sleep(1000);
        driver.findElement(By.id("j-official-terms")).click();
        Thread.sleep(1000);
        driver.findElement(By.partialLinkText("手机号登录")).click();
        Thread.sleep(1000);
        driver.findElement(By.partialLinkText("密码登录")).click();
        Thread.sleep(1000);
        //输入账号密码
        driver.findElement(By.xpath("//input[@placeholder=\"请输入手机号\"]")).sendKeys("");
        driver.findElement(By.xpath("//input[@placeholder=\"请输入密码\"]")).sendKeys("");
        //点击登录
        xpathClick(driver, "//a/div[@class=\"tan2MIhq\"]");
        Thread.sleep(3000);
        driver.findElement(By.partialLinkText("排行榜")).click();

        //2. 点击“热歌榜”，选择热歌榜第一名的歌曲。
        //切换到iframe内容
        driver.switchTo().frame("g_iframe");
        //点击热歌榜
        xpathClick(driver,"/html/body/div[3]/div[1]/div[1]/ul[1]/li[4]/div[1]/p[1]/a[1]");
        Thread.sleep(1000);
        //点击第一首歌
        xpathClick(driver,"/html/body/div[3]/div[2]/div/div[2]/div[2]/div/div[1]/table/tbody/tr[1]/td[2]/div/div/a/img");
        Thread.sleep(1000);
        //3. 点击“收藏”按钮，新建歌单。

        //点击收藏
        xpathClick(driver,"//*[@id=\"content-operation\"]/a[3]/i");
        Thread.sleep(1000);
        //点击新建歌单
        xpathClick(driver,"/html/body/div[11]/div[2]/div/div[1]");
        Thread.sleep(1000);

        //4. 输入歌单名为“Test”，并点击“确定”，完成歌单的创建和收藏
        xpathSendKeys(driver,"/html/body/div[11]/div[2]/div/p[1]/input","Test");
        Thread.sleep(1000);
        //点击新建
        xpathClick(driver,"/html/body/div[11]/div[2]/div/div[2]/a[1]");
        Thread.sleep(1000);

        //5. 网页下拉，在评论框中输入“真棒”，然后点击“评论”按钮。对下面精彩评论中的第一条评论进行点赞操作。
        String script = "return arguments[0].scrollIntoView();";//return可以不写
        WebElement e = driver.findElement(By.xpath("/html/body/div[3]/div[1]/div/div/div[2]/div/div[2]/div[1]/div[2]/div/div/div[1]/textarea"));
        ((JavascriptExecutor) driver).executeScript(script, e);
        Thread.sleep(1000);
        e.sendKeys("真棒");
        xpathClick(driver,"/html/body/div[3]/div[1]/div/div/div[2]/div/div[2]/div[1]/div[2]/div/div/div[2]/a");
        Thread.sleep(1000);
        //6. 点击右下角“锁”标志，将播放器栏锁定，修改播放模式（随意修改，点击 一下即可）
        //切换到默认内容
        driver.switchTo().defaultContent();
        //点击锁形按钮
        e = driver.findElement(By.xpath("//*[@id=\"g_player\"]"));
        action.moveToElement(e).perform();
        e = driver.findElement(By.xpath("/html/body/div[2]/div/div[1]/div[1]/a"));
        action.moveToElement(e).perform();
        e.click();
        Thread.sleep(1000);
        //更改播放模式（更改成什么模式都可以，只要点击到控件即可）
        e = driver.findElement(By.xpath("/html/body/div[2]/div/div[4]/div[5]/a[2]"));
        action.moveToElement(e).perform();
        e.click();
        Thread.sleep(1000);

        //7. 点击“歌单”，选择第一个歌单。
        //网页滚动
        action.sendKeys(Keys.HOME).perform();
        Thread.sleep(1000);
        //点击歌单
        xpathClick(driver, "/html/body/div[1]/div[3]/div/ul/li[3]/a/em");
        Thread.sleep(1000);
        //点击第一个歌单
        //切换到iframe内容
        driver.switchTo().frame("g_iframe");
        //点击第一个歌单
        xpathClick(driver, "//*[@id=\"m-pl-container\"]/li[1]/div/a");
        Thread.sleep(1000);
        //8. 点击“播放”按钮，播放清单。点击下方的播放列表，点击“清除”，清空播放列表，然后关闭播放列表。

        //点击播放
        xpathClick(driver, "//*[@id=\"content-operation\"]/a[1]");
        Thread.sleep(1000);
        //点击播放列表
        //切换到默认内容
        driver.switchTo().defaultContent();
        xpathClick(driver, "//*[@id=\"g_player\"]/div[5]/span/a");
        Thread.sleep(1000);
        //点击清除
        xpathClick(driver, "//*[@id=\"g_playlist\"]/div[1]/div/a[2]");
        Thread.sleep(1000);
        //关闭播放列表
        xpathClick(driver,"//*[@id=\"g_playlist\"]/div[1]/div/span[2]");
        Thread.sleep(4000);


        //9. 点击上方菜单中的“商城”。
        //点击商城
        xpathClick(driver, "//*[@id=\"g-topbar\"]/div[1]/div/ul/li[4]/span/a/em");
        //切换句柄
        Set<String> h = driver.getWindowHandles();
        String[] b = setArray(h);
        driver.switchTo().window(b[0]);
        String h2 = driver.getWindowHandle();
        System.out.println(h2);
        System.out.println(h);

        //10． 在商城中搜索“薛之谦”，将搜索结果按照价格从低到高排序，选择第一个商品。
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div/div[1]/form/input")).sendKeys("薛之谦",Keys.ENTER);
        Thread.sleep(3000);
        //选择“价格从低到高”
        xpathClick(driver, "//*[@id=\"search-list\"]/div[1]/div/div/ul/li[3]/a");
        //点击第一个产品的照片进入详情页
        xpathClick(driver, "//*[@id=\"search-list\"]/div[1]/ul/li[1]/div/a/img");

        //11. 商品数量加一，将该商品加入购物车，并点击右侧的“购物车”按钮进入购物车。
        h = driver.getWindowHandles();
        b = setArray(h);
        driver.switchTo().window(b[0]);
        h2 = driver.getWindowHandle();
        System.out.println(h2);
        System.out.println(h);
        xpathClick(driver, "//*[@id=\"digital-counter-new\"]/div[1]/a[2]");
        xpathClick(driver, "//*[@id=\"buyFlag\"]/a[2]");
        xpathClick(driver, "//*[@id=\"m-top\"]/div[4]/span");

        //12. 点击“结算”按钮。
        h = driver.getWindowHandles();
        b = setArray(h);
        driver.switchTo().window(b[0]);
        h2 = driver.getWindowHandle();
        System.out.println(h2);
        System.out.println(h);
        xpathClick(driver, "//*[@id=\"bottom\"]/div/div[3]");

        //退出浏览器
        driver.quit();
    }

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "/webTest/src/test/resources/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        Actions action = new Actions(driver);
        try {
            test(driver, action);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            driver.quit();
        }//driver.close()关闭当前窗口driver.quit()退出驱动关闭所有窗口
    }
}
